package kks;

import static org.junit.Assert.assertEquals;
import kks.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.BattlefieldSpecification;

/**
 * Verifies that the CounterAll's gun doesn't face the wall it is adjacent to.
 * 
 * @author Kelli Sawai
 * 
 */
public class TestGunPosition extends RobotTestBed {

  BattlefieldSpecification field = new BattlefieldSpecification();
  // Height of the battlefield
  double fieldHeight = field.getHeight();
  // Width of the battlefield
  double fieldWidth = field.getWidth();
  // Number of times the gun is in a facing a bad direction
  int badGunDirection = 0;

  /**
   * Specifies that SpinBot and CounterAll are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SpinBot,kks.CounterAll";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Checks to see if robots gun every points towards the wall it is adjacent to.
   * 
   * @param event information stored about the robots at the end of a turn
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // IRobotSnapshot robot creation taken from David Mau
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double gunHeading = robot.getGunHeading();
    double robotX = robot.getX();
    double robotY = robot.getY();

    // robot along left side of battlefield
    if (robotX == 0 && gunHeading > 180 && gunHeading < 360) {
      badGunDirection++;
    }

    // robot along right side of battlefield
    else if (robotX == fieldWidth && gunHeading > 0 && gunHeading < 180) {
      badGunDirection++;
    }

    // robot along top side of battlefield
    else if (robotY == fieldHeight && (gunHeading >= 0 && gunHeading < 90)
        || (gunHeading < 270 && gunHeading > 360)) {
      badGunDirection++;
    }

    // robot along bottom side of battlefield
    else if (robotY == 0 && gunHeading > 90 && gunHeading < 270) {
      badGunDirection++;
    }
  }

  /**
   * The actual test, which asserts that CounterAll has its gun pointed away from the adjacent wall
   * all of the time.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertEquals("Check robots gun isn't facing away from battlefield.", badGunDirection, 0);
  }
}
